﻿using System;
using System.Collections.Generic;

namespace Mozi.Live.SDP
{
    public class SDPAttributeFMTP
    {
        public byte PayloadType { get; set; }
        public Dictionary<string,string> Params { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string pm = $"fmtp:{PayloadType}";
            if (Params != null)
            {
                foreach(var kp in Params)
                {
                    pm+=$"; {kp.Key}={kp.Value}";
                }
            }
            return pm;
        }

        public SDPAttributeFMTP Parse(string text)
        {
            SDPAttributeFMTP fmtp = new SDPAttributeFMTP();
            string[] items = text.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            fmtp.PayloadType = byte.Parse(items[0].Replace("fmtp", "").Trim());
            fmtp.Params = new Dictionary<string, string>();
            for(int i = 1; i < items.Length; i++)
            {
                string[] kp = items[i].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                fmtp.Params.Add(kp[0], kp.Length>1?kp[1]:null);
            }
            return fmtp;
        }
    }
}
